
#include <math.h>
#include "Vector2DList.h"

Vector2DList::Vector2DList()
{
	head = NULL;
}
Vector2DList::Vector2DList(Vector2D * headVector2D)
{
	head = new List<Vector2D>(headVector2D, NULL);
}
Vector2DList::~Vector2DList()
{
	printf("deleting vector list\n");
	List<Vector2D> * search = head;
	int i = 0;
	while(search)
	{
		printf("vector number %d\n",i);
		i++;
		search = search->RemoveList();
	}
	head = NULL;
	printf("done deleting vectors\n");
}
List<Vector2D> * Vector2DList::Add(Vector2D * payload)
{
	head = new List<Vector2D>(payload, head);
	return head;
}
void Vector2DList::Remove(Vector2D point)
{
	List<Vector2D> * search = head;
	while(search)
	{
		if(search->payload && search->payload->dist(point) < 0.001)
		{
			search = search->Remove();
		}
		else
			search = search->next;
	}	
}

void Vector2DList::Remove(Vector2D * point)
{
	List<Vector2D> * search = head;
	while(search)
	{
		if(search->payload && search->payload ==  point)
		{
			search = search->Remove();
		}
		else
			search = search->next;
	}	
}


void Vector2DList::Rotate(float sinTheta, float cosTheta)
{
	List<Vector2D> * search = head;
	while(search)
	{
		if(search->payload)
		{
			//printf("Vector2DList point pre rotate = (%f, %f)\n",search->payload->x,search->payload->y); 
			float newX = search->payload->x * cosTheta - search->payload->y * sinTheta;
			float newY = search->payload->y * cosTheta + search->payload->x * sinTheta;
			search->payload->x = newX;
			search->payload->y = newY;
			//printf("Vector2DList point post rotate = (%f, %f)\n",search->payload->x,search->payload->y);
		}
		search = search->next;
	}	
}

void Vector2DList::Rotate(float theta)
{
	
	if(fabs(theta) < 0.000001)
		return;
		
	float sinTheta = sin(theta);
	float cosTheta = cos(theta);
	
	Rotate(sinTheta, cosTheta);
}

void Vector2DList::Rotate(float sinTheta, float cosTheta, Vector2D axisPoint, Vector2D objectCenter)
{
	List<Vector2D> * search = head;
	while(search)
	{
		if(search->payload)
		{
			//printf("Vector2DList point pre rotate = (%f, %f)\n",search->payload->x,search->payload->y);
			*search->payload =  *search->payload - axisPoint;
			float newX = search->payload->x * cosTheta - search->payload->y * sinTheta;
			float newY = search->payload->y * cosTheta + search->payload->x * sinTheta;
			search->payload->x = newX;
			search->payload->y = newY;
			
			*search->payload =  *search->payload - objectCenter;
			//printf("Vector2DList point post rotate = (%f, %f)\n",search->payload->x,search->payload->y);
		}
		search = search->next;
	}
}
